package org.squabble.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.squabble.dao.AccountDAO;
import org.squabble.domain.Account;

@Service
public class SearchService {

	@Autowired
	AccountDAO accountDAO;

	public List<Account> searchAccounts(String name, String email,
			Long privilegeId) {
		return searchAccounts(name, email, privilegeId, MatchMode.ANYWHERE);
	}

	public List<Account> searchAccounts(String name, String email,
			Long privilegeId, MatchMode matchMode) {
		return accountDAO.searchAccounts(name, email, privilegeId, matchMode);
	}

	public List<Account> getNewAccounts() {
		return accountDAO.findAccounts(Order.desc("sourceDate"), 10);
	}

	public List<Account> getAccounts(String search) {
		Set<Account> accountSet = new HashSet<Account>(searchAccounts(search, null, null, MatchMode.START));
		return new ArrayList<Account>(accountSet);
	}

	public List<Account> getAccounts() {
		List<Account> accounts = accountDAO.list();
		//Collections.sort(accounts);
		return accounts;
	}
	
}
